<?php
/*
 * Created on Sep 13, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("./configure/configure.php");		//--> global var
include_once("Smarty.class.php");				//--> out template
include_once("Common.php");

include_once("Strings.php");					//--> String utils

include_once("customerSession.inc.php");
include_once("Session.php");					//-- Session
include_once ("Validation.php");				//-- Validation

include_once("orders/Cart.php");					//--> Cart
include_once("orders/CartProduct.php");				//--> Cart
include_once("orders/CartProductAttribute.php");	//--> Cart
include_once("orders/OrdersInfo.php");				//-- for payment

session_start();


$common 		= new Common();
$objStrings 	= new Strings();

$objSession		= new Session(DB_TAG_SYSTEM, SESSION_TABLE_NAME);	//-- session

$objCart 		= new Cart();		//--> Cart
$objValidation	= new Validation();		//--> Validation

$isLogin	= false;

if($objSession->exist()) {
	$isLogin	= true;
	
	$location 	= "./checkout.php";
				
	header("Location: $location");
	exit;
}

if($_SESSION['cart']){
	$objCart = unserialize($_SESSION['cart']);
}

if(isset($_POST['email'])){
	$email 	= $_POST['email'];
	
	if(empty($email)){
		$error_message = "E-mail address is required.";
	}else if(!$objValidation->isEmail($email)){
		$error_message = 'The email format invalid.';
	}else{
		$customer = $common->getRow(DB_TAG_SYSTEM, "SELECT id, paypal_ec, lastname, isAutoRegister FROM customers WHERE email ='$email'");
	   	// see if we found a record

	    if (count($customer) > 0 && $customer['isAutoRegister'] == 1) {
	    	// login
			
			$arrdata = array();
			$arrdata['uname'] = $customer['lastname'];		//--> add login name to session
		
			if(is_array($objSession->start($customer['id'], $arrdata))){
			}
	    }else if(count($customer) > 0){
	    	$error_message = "E-mail address is existed. Please login.";
	    }
	}
	
	if(empty($error_message)){
		if(isset($_SESSION['ordersInfo'])){
			$objOrdersInfo = unserialize($_SESSION['ordersInfo']);
		}else{
			$objOrdersInfo = new OrdersInfo();
		}
		
		$objOrdersInfo->setEmail($email);
		
		$_SESSION['ordersInfo'] = serialize($objOrdersInfo);
		
		$location 	= "./checkout.php";
				
		header("Location: $location");
		exit;
		
 	}
}


/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir 	= TEMPLATE_DIR;
$smarty->compile_dir  	= COMPILE_DIR;
$smarty->cache_dir  	= CACHE_DIR;

$smarty->caching 		= CACHING;
$smarty->cache_lifetime = CACHE_LIFETIME;

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL);

$smarty->assign('error_message', $error_message);
$smarty->assign('error_message_login', $_GET['error_message_login']);


$smarty->assign('objCart', $objCart);
$smarty->assign('objStrings', $objStrings);

$smarty->assign('isLogin', $isLogin);

$smarty->display('preCheckout.html');
?>
